import Vue from 'vue'
import VueRouter from 'vue-router'

import Index from '../views/myproj/Index.vue'

import Home from '../views/myproj/Home.vue'
import Search from '../views/myproj/Search.vue'
import Profile from '../views/myproj/Profile.vue'
import Details from '../views/myproj/Details.vue'
import Page404 from '../views/myproj/Page404.vue'

import User from '../views/testapi/User.vue'

import Comp1 from '../views/myproj/Comp1.vue'
import Comp2 from '../views/myproj/Comp2.vue'
import Comp3 from '../views/myproj/Comp3.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index/home/001'
  },

  {
    path: '/index',
    component: Index,
    name: 'index',
    children: [
      {
        path: 'home/:id',
        component: Home,
        name: 'home',
        props: true
      },
      {
        path: 'search',
        components: {
          default: Search,
          comp1: Comp1,
          comp2: Comp2,
          comp3: Comp3
        },
        name: 'search',
        alias: 'sss'
      },
      {
        path: 'profile',
        component: Profile,
        name: 'profile',
      },
    ]
  },

  {
    path: '/details/:id',
    components: {
      default: Details,
      comp1: Comp1
    },
    name: 'details',
    // props: (route) => {
    //   return {
    //     aaa: 900,
    //     id: route.params.id
    //   }
    // }
    props: {
      default: true,
      comp1: true
    },
    beforeEnter(to, from, next) {
      next()
    }
  },

  {
    path: '/users',
    component: User,
    name: 'users'
  },

  {
    path: '*',
    component: Page404
  },
]

const router = new VueRouter({
  routes,
  mode: 'hash'
})

// 全局守卫
// router.beforeEach((to, from, next) => {
//   if (to.fullPath === '/index/home/003') {
//     next()
//   } else {
//     alert('你算错了')
//   }
// })

// router.afterEach((to, from) => {
//   if(confirm('你真的要离开吗？')) {

//   }
// })

export default router